package cn.jhc.bookbrowser;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by luyanfei on 16-2-27.
 */
public class BookPagerAdapter extends FragmentPagerAdapter {
    private String[] titles;
    private String[] descriptions;
    private int[] imageIds;

    public BookPagerAdapter(FragmentManager fm, Activity activity) {
        super(fm);
        titles = activity.getResources().getStringArray(R.array.bookTitles);
        descriptions = activity.getResources().getStringArray(R.array.bookDescs);
        TypedArray images = activity.getResources().obtainTypedArray(R.array.bookImages);
        imageIds = new int[images.length()];
        for (int i = 0; i < images.length(); i++) {
            imageIds[i] = images.getResourceId(i,0);
        }
    }

    @Override
    public Fragment getItem(int position) {
        Bundle arguments = new Bundle();
        arguments.putString(BookFragment.BOOK_TITLE, titles[position]);
        arguments.putString(BookFragment.BOOK_DESCRIPTION, descriptions[position]);
        arguments.putInt(BookFragment.TOP_IMAGE, imageIds[position]);
        BookFragment bookFragment = new BookFragment();
        bookFragment.setArguments(arguments);
        return bookFragment;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
